function out = calculateDuration2020(inputOrigin, inputDeriv, sizeFOV, canlocation, startPoint, interestedlocation, outputfilename, clim, handles)

xsize = inputOrigin.xsize;
ysize = inputOrigin.ysize;

pacingIntervalFrame = inputOrigin.pacingIntervalFrame;
nPulse = inputOrigin.nPulse;
StopFramePerBeat = (1:nPulse)*pacingIntervalFrame;
mSecForFrame = inputOrigin.mSecForFrame;

rawMaxI = inputOrigin.maxSignalIData; %ID(frame) of maximum signal
rawMinI = inputOrigin.minSignalIData; %ID(frame) of minimum signal
rawData2 = inputOrigin.dataWOPacingSignalNorm; %Normalized data with removing pacing signals
rawData = inputOrigin.dataWOPacingSignalEachBeatNorm; 
rawActMapDataPerBeat = inputDeriv.maxSignalIData1; %ID of maximum d(signal)/dt

rawMaxSignal = inputOrigin.maxSignal;
rawMinSignal = inputOrigin.minSignal;
rawAmplitude = rawMaxSignal - rawMinSignal;

rawData90 = rawData-0.1;
rawData80 = rawData-0.2;
rawData50 = rawData-0.5;
rawData30 = rawData-0.7;

rawData70 = rawData-0.3;
rawData60 = rawData-0.4;
rawData40 = rawData-0.6;
rawData20 = rawData-0.8;
rawData10 = rawData-0.9;

numberDataCheck = 2;

rawData90Check = zeros((size(rawData,1)),size(rawData,2));
rawData80Check = zeros((size(rawData,1)),size(rawData,2));
rawData50Check = zeros((size(rawData,1)),size(rawData,2));
rawData30Check = zeros((size(rawData,1)),size(rawData,2));

rawData70Check = zeros((size(rawData,1)),size(rawData,2));
rawData60Check = zeros((size(rawData,1)),size(rawData,2));
rawData40Check = zeros((size(rawData,1)),size(rawData,2));
rawData20Check = zeros((size(rawData,1)),size(rawData,2));
rawData10Check = zeros((size(rawData,1)),size(rawData,2));


for j=1:size(rawData,2)
    j
    for i=1:(size(rawData,1))

        if(i-pacingIntervalFrame*fix((i-1)/pacingIntervalFrame))>(pacingIntervalFrame-numberDataCheck)
            temp90 = rawData90(i,j)*rawData90(i+numberDataCheck-pacingIntervalFrame,j);
            temp80 = rawData80(i,j)*rawData80(i+numberDataCheck-pacingIntervalFrame,j);
            temp50 = rawData50(i,j)*rawData50(i+numberDataCheck-pacingIntervalFrame,j);
            temp30 = rawData30(i,j)*rawData30(i+numberDataCheck-pacingIntervalFrame,j);

            temp70 = rawData70(i,j)*rawData70(i+numberDataCheck-pacingIntervalFrame,j);
            temp60 = rawData60(i,j)*rawData60(i+numberDataCheck-pacingIntervalFrame,j);
            temp40 = rawData40(i,j)*rawData40(i+numberDataCheck-pacingIntervalFrame,j);
            temp20 = rawData20(i,j)*rawData20(i+numberDataCheck-pacingIntervalFrame,j);
            temp10 = rawData10(i,j)*rawData10(i+numberDataCheck-pacingIntervalFrame,j);
        else
        
            temp90 = rawData90(i,j)*rawData90(i+numberDataCheck,j);
            temp80 = rawData80(i,j)*rawData80(i+numberDataCheck,j);
            temp50 = rawData50(i,j)*rawData50(i+numberDataCheck,j);
            temp30 = rawData30(i,j)*rawData30(i+numberDataCheck,j);

            temp70 = rawData70(i,j)*rawData70(i+numberDataCheck,j);
            temp60 = rawData60(i,j)*rawData60(i+numberDataCheck,j);
            temp40 = rawData40(i,j)*rawData40(i+numberDataCheck,j);
            temp20 = rawData20(i,j)*rawData20(i+numberDataCheck,j);
            temp10 = rawData10(i,j)*rawData10(i+numberDataCheck,j);
        end

        if(temp90<0) 
            rawData90Check(i,j)=1; 
        end
        if(temp80<0) 
            rawData80Check(i,j)=1; 
        end
        if(temp50<0) 
            rawData50Check(i,j)=1; 
        end
        if(temp30<0) 
            rawData30Check(i,j)=1; 
        end

        if(temp70<0) 
            rawData70Check(i,j)=1; 
        end
        if(temp60<0) 
            rawData60Check(i,j)=1; 
        end
        if(temp40<0) 
            rawData40Check(i,j)=1; 
        end
        if(temp20<0) 
            rawData20Check(i,j)=1; 
        end
        if(temp10<0) 
            rawData10Check(i,j)=1; 
        end
    end
end

frame90 = zeros(nPulse,size(rawData,2)); 
frame80 = zeros(nPulse,size(rawData,2));
frame50 = zeros(nPulse,size(rawData,2));
frame30 = zeros(nPulse,size(rawData,2));
frame90_1 = zeros(nPulse,size(rawData,2)); 
frame80_1 = zeros(nPulse,size(rawData,2));
frame50_1 = zeros(nPulse,size(rawData,2));
frame30_1 = zeros(nPulse,size(rawData,2));
duration90 = zeros(nPulse,size(rawData,2));
duration80 = zeros(nPulse,size(rawData,2));
duration50 = zeros(nPulse,size(rawData,2));
duration30 = zeros(nPulse,size(rawData,2));
duration90_1 = zeros(nPulse,size(rawData,2));
duration80_1 = zeros(nPulse,size(rawData,2));
duration50_1 = zeros(nPulse,size(rawData,2));
duration30_1 = zeros(nPulse,size(rawData,2));

frame70 = zeros(nPulse,size(rawData,2)); 
frame60 = zeros(nPulse,size(rawData,2));
frame40 = zeros(nPulse,size(rawData,2));
frame20 = zeros(nPulse,size(rawData,2));
frame10 = zeros(nPulse,size(rawData,2));
frame70_1 = zeros(nPulse,size(rawData,2)); 
frame60_1 = zeros(nPulse,size(rawData,2));
frame40_1 = zeros(nPulse,size(rawData,2));
frame20_1 = zeros(nPulse,size(rawData,2));
frame10_1 = zeros(nPulse,size(rawData,2));
duration70 = zeros(nPulse,size(rawData,2));
duration60 = zeros(nPulse,size(rawData,2));
duration40 = zeros(nPulse,size(rawData,2));
duration20 = zeros(nPulse,size(rawData,2));
duration10 = zeros(nPulse,size(rawData,2));
duration70_1 = zeros(nPulse,size(rawData,2));
duration60_1 = zeros(nPulse,size(rawData,2));
duration40_1 = zeros(nPulse,size(rawData,2));
duration20_1 = zeros(nPulse,size(rawData,2));
duration10_1 = zeros(nPulse,size(rawData,2));

for j=1:size(rawData,2)
    j
    for k=1:nPulse

%         temp90perBeat = rawData90Check((rawMaxI(k,j):(k*pacingIntervalFrame-numberDataCheck)),j);
%         temp80perBeat = rawData80Check((rawMaxI(k,j):(k*pacingIntervalFrame-numberDataCheck)),j);
%         temp50perBeat = rawData50Check((rawMaxI(k,j):(k*pacingIntervalFrame-numberDataCheck)),j);
%         temp30perBeat = rawData30Check((rawMaxI(k,j):(k*pacingIntervalFrame-numberDataCheck)),j);
        
        rawData90perBeat = rawData90([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData80perBeat = rawData80([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData50perBeat = rawData50([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData30perBeat = rawData30([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);

        [rawData90Max, rawData90MaxI] = max(rawData90perBeat);
        [rawData80Max, rawData80MaxI] = max(rawData80perBeat);
        [rawData50Max, rawData50MaxI] = max(rawData50perBeat);
        [rawData30Max, rawData30MaxI] = max(rawData30perBeat);

        [rawData90Min, rawData90MinI] = min(rawData90perBeat);
        [rawData80Min, rawData80MinI] = min(rawData80perBeat);
        [rawData50Min, rawData50MinI] = min(rawData50perBeat);
        [rawData30Min, rawData30MinI] = min(rawData30perBeat);
        
        rawData90CheckperBeat = rawData90Check([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData80CheckperBeat = rawData80Check([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData50CheckperBeat = rawData50Check([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData30CheckperBeat = rawData30Check([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);

        frame90(k,j) = (rawMaxI(k,j) + mean(find(rawData90CheckperBeat(rawData90MaxI:(rawData90MinI-numberDataCheck))==1))-1);
        frame80(k,j) = (rawMaxI(k,j) + mean(find(rawData80CheckperBeat(rawData80MaxI:(rawData80MinI-numberDataCheck))==1))-1);
        frame50(k,j) = (rawMaxI(k,j) + mean(find(rawData50CheckperBeat(rawData50MaxI:(rawData50MinI-numberDataCheck))==1))-1);
        frame30(k,j) = (rawMaxI(k,j) + mean(find(rawData30CheckperBeat(rawData30MaxI:(rawData30MinI-numberDataCheck))==1))-1);

%         frame90(k,j) = (rawMaxI(k,j) + mean(find(temp90perBeat==1))-1);
%         frame80(k,j) = (rawMaxI(k,j) + mean(find(temp80perBeat==1))-1);
%         frame50(k,j) = (rawMaxI(k,j) + mean(find(temp50perBeat==1))-1);
%         frame30(k,j) = (rawMaxI(k,j) + mean(find(temp30perBeat==1))-1);

        frame90_1(k,j) = (frame90(k,j)-pacingIntervalFrame*(k-1));
        frame80_1(k,j) = (frame80(k,j)-pacingIntervalFrame*(k-1));
        frame50_1(k,j) = (frame50(k,j)-pacingIntervalFrame*(k-1));
        frame30_1(k,j) = (frame30(k,j)-pacingIntervalFrame*(k-1));

        duration90(k,j) = (frame90_1(k,j) - rawActMapDataPerBeat(k,j))*mSecForFrame; 
        duration80(k,j) = (frame80_1(k,j) - rawActMapDataPerBeat(k,j))*mSecForFrame;
        duration50(k,j) = (frame50_1(k,j) - rawActMapDataPerBeat(k,j))*mSecForFrame;
        duration30(k,j) = (frame30_1(k,j) - rawActMapDataPerBeat(k,j))*mSecForFrame;
        
        if(duration90(k,j)<=0)
            duration90(k,j) = duration90(k,j) + pacingIntervalFrame*mSecForFrame;
        end
        if(duration80(k,j)<=0)
            duration80(k,j) = duration80(k,j) + pacingIntervalFrame*mSecForFrame;
        end
        if(duration50(k,j)<=0)
            duration50(k,j) = duration50(k,j) + pacingIntervalFrame*mSecForFrame;
        end
        if(duration30(k,j)<=0)
            duration30(k,j) = duration30(k,j) + pacingIntervalFrame*mSecForFrame;
        end

%         clear temp90perBeat
%         clear temp80perBeat
%         clear temp50perBeat
%         clear temp30perBeat

        clear rawData90perBeat
        clear rawData80perBeat
        clear rawData50perBeat
        clear rawData30perBeat
        
        
%         temp70perBeat = rawData70Check((rawMaxI(k,j):(k*pacingIntervalFrame-numberDataCheck)),j);
%         temp60perBeat = rawData60Check((rawMaxI(k,j):(k*pacingIntervalFrame-numberDataCheck)),j);
%         temp40perBeat = rawData40Check((rawMaxI(k,j):(k*pacingIntervalFrame-numberDataCheck)),j);
%         temp20perBeat = rawData20Check((rawMaxI(k,j):(k*pacingIntervalFrame-numberDataCheck)),j);
%         temp10perBeat = rawData10Check((rawMaxI(k,j):(k*pacingIntervalFrame-numberDataCheck)),j);

        rawData70perBeat = rawData70([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData60perBeat = rawData60([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData40perBeat = rawData40([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData20perBeat = rawData20([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData10perBeat = rawData10([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);

        [rawData70Max, rawData70MaxI] = max(rawData70perBeat);
        [rawData60Max, rawData60MaxI] = max(rawData60perBeat);
        [rawData40Max, rawData40MaxI] = max(rawData40perBeat);
        [rawData20Max, rawData20MaxI] = max(rawData20perBeat);
        [rawData10Max, rawData10MaxI] = max(rawData10perBeat);

        [rawData70Min, rawData70MinI] = min(rawData70perBeat);
        [rawData60Min, rawData60MinI] = min(rawData60perBeat);
        [rawData40Min, rawData40MinI] = min(rawData40perBeat);
        [rawData20Min, rawData20MinI] = min(rawData20perBeat);
        [rawData10Min, rawData10MinI] = min(rawData10perBeat);
        
        rawData70CheckperBeat = rawData70Check([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData60CheckperBeat = rawData60Check([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData40CheckperBeat = rawData40Check([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData20CheckperBeat = rawData20Check([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        rawData10CheckperBeat = rawData10Check([rawMaxI(k,j):(k*pacingIntervalFrame) ((k-1)*pacingIntervalFrame+1):(rawMaxI(k,j)-1)],j);
        
        frame70(k,j) = (rawMaxI(k,j) + mean(find(rawData70CheckperBeat(rawData70MaxI:(rawData70MinI-numberDataCheck))==1))-1);
        frame60(k,j) = (rawMaxI(k,j) + mean(find(rawData60CheckperBeat(rawData60MaxI:(rawData60MinI-numberDataCheck))==1))-1);
        frame40(k,j) = (rawMaxI(k,j) + mean(find(rawData40CheckperBeat(rawData40MaxI:(rawData40MinI-numberDataCheck))==1))-1);
        frame20(k,j) = (rawMaxI(k,j) + mean(find(rawData20CheckperBeat(rawData20MaxI:(rawData20MinI-numberDataCheck))==1))-1);
        frame10(k,j) = (rawMaxI(k,j) + mean(find(rawData10CheckperBeat(rawData10MaxI:(rawData10MinI-numberDataCheck))==1))-1);
    
%         frame70(k,j) = (rawMaxI(k,j) + mean(find(temp70perBeat==1))-1);
%         frame60(k,j) = (rawMaxI(k,j) + mean(find(temp60perBeat==1))-1);
%         frame40(k,j) = (rawMaxI(k,j) + mean(find(temp40perBeat==1))-1);
%         frame20(k,j) = (rawMaxI(k,j) + mean(find(temp20perBeat==1))-1);
%         frame10(k,j) = (rawMaxI(k,j) + mean(find(temp10perBeat==1))-1);

        frame70_1(k,j) = (frame70(k,j)-pacingIntervalFrame*(k-1));
        frame60_1(k,j) = (frame60(k,j)-pacingIntervalFrame*(k-1));
        frame40_1(k,j) = (frame40(k,j)-pacingIntervalFrame*(k-1));
        frame20_1(k,j) = (frame20(k,j)-pacingIntervalFrame*(k-1));
        frame10_1(k,j) = (frame10(k,j)-pacingIntervalFrame*(k-1));

        duration70(k,j) = (frame70_1(k,j) - rawActMapDataPerBeat(k,j))*mSecForFrame; 
        duration60(k,j) = (frame60_1(k,j) - rawActMapDataPerBeat(k,j))*mSecForFrame;
        duration40(k,j) = (frame40_1(k,j) - rawActMapDataPerBeat(k,j))*mSecForFrame;
        duration20(k,j) = (frame20_1(k,j) - rawActMapDataPerBeat(k,j))*mSecForFrame;
        duration10(k,j) = (frame10_1(k,j) - rawActMapDataPerBeat(k,j))*mSecForFrame;
        
        if(duration70(k,j)<=0)
            duration70(k,j) = duration70(k,j) + pacingIntervalFrame*mSecForFrame;
        end
        if(duration60(k,j)<=0)
            duration60(k,j) = duration60(k,j) + pacingIntervalFrame*mSecForFrame;
        end
        if(duration40(k,j)<=0)
            duration40(k,j) = duration40(k,j) + pacingIntervalFrame*mSecForFrame;
        end
        if(duration20(k,j)<=0)
            duration20(k,j) = duration20(k,j) + pacingIntervalFrame*mSecForFrame;
        end
        if(duration10(k,j)<=0)
            duration10(k,j) = duration10(k,j) + pacingIntervalFrame*mSecForFrame;
        end

%         clear temp70perBeat
%         clear temp60perBeat
%         clear temp40perBeat
%         clear temp20perBeat
%         clear temp10perBeat

        clear rawData70perBeat
        clear rawData60perBeat
        clear rawData40perBeat
        clear rawData20perBeat
        clear rawData10perBeat

    end
end

duration90_tAvg = mean(duration90);
duration80_tAvg = mean(duration80);
duration50_tAvg = mean(duration50);
duration30_tAvg = mean(duration30);

duration70_tAvg = mean(duration70);
duration60_tAvg = mean(duration60);
duration40_tAvg = mean(duration40);
duration20_tAvg = mean(duration20);
duration10_tAvg = mean(duration10);

figure(201)
plot(rawData(:,5050))
hold on
plot(rawMaxI(:,5050),rawData(rawMaxI(:,5050),5050), 'go')
plot(StopFramePerBeat,rawData(StopFramePerBeat,5050), 'go')
plot(find(rawData90Check(:,5050)==1),rawData(find(rawData90Check(:,5050)==1),5050), 'ro')
plot(find(rawData80Check(:,5050)==1),rawData(find(rawData80Check(:,5050)==1),5050), 'r*')
plot(find(rawData50Check(:,5050)==1),rawData(find(rawData50Check(:,5050)==1),5050), 'rx')
plot(find(rawData60Check(:,5050)==1),rawData(find(rawData60Check(:,5050)==1),5050), 'r^')
plot(find(rawData30Check(:,5050)==1),rawData(find(rawData30Check(:,5050)==1),5050), 'r+')

fileName1 = [outputfilename(1:(length(outputfilename)-8)) '50_50_traces1.fig'];
saveas(gcf, fileName1, 'fig');

if(get(handles.autoClose, 'Value'))
    close figure 201
end

figure(202)
plot(rawData(:,5050))
hold on
plot(rawMaxI(:,5050),rawData(rawMaxI(:,5050),5050), 'go')
plot(StopFramePerBeat,rawData(StopFramePerBeat,5050), 'go')
plot(frame90(:,5050),0.1*ones(length(frame90(:,5050)),1), 'ro')
plot(frame80(:,5050),0.2*ones(length(frame80(:,5050)),1), 'r*')
plot(frame60(:,5050),0.4*ones(length(frame60(:,5050)),1), 'r^')
plot(frame50(:,5050),0.5*ones(length(frame50(:,5050)),1), 'rx')
plot(frame30(:,5050),0.7*ones(length(frame30(:,5050)),1), 'r+')
% plot(round(frame90(:,5050)),rawData(round(frame90(:,5050)),5050), 'ro')
% plot(round(frame80(:,5050)),rawData(round(frame80(:,5050)),5050), 'r*')
% plot(round(frame50(:,5050)),rawData(round(frame50(:,5050)),5050), 'rx')
% plot(round(frame30(:,5050)),rawData(round(frame30(:,5050)),5050), 'r+')

fileName1 = [outputfilename(1:(length(outputfilename)-8)) '50_50_traces.fig'];
saveas(gcf, fileName1, 'fig');
if(get(handles.autoClose, 'Value'))
    close figure 202
end

for i=1:size(canlocation,1)
    figure(191+i*20)
    %interested plots
    for ik=1:7       
        index = round(xsize*(canlocation(i,1)+canlocation(i,2))/2+(startPoint+interestedlocation)/2)-30+10*(ik-1);
        plot(((1:size(rawData,1))-1)*mSecForFrame/1000, rawData(:,index))
        if(ik == 1)
            hold on
        end
        plot((rawMaxI(:,index)-1)*mSecForFrame/1000,rawData(rawMaxI(:,index),index), 'go')
        plot((StopFramePerBeat-1)*mSecForFrame/1000,rawData(StopFramePerBeat,index), 'go')
        plot((frame90(:,index)-1)*mSecForFrame/1000,0.1*ones(length(frame90(:,index)),1), 'ro')
        plot((frame80(:,index)-1)*mSecForFrame/1000,0.2*ones(length(frame80(:,index)),1), 'r*')
        plot((frame60(:,index)-1)*mSecForFrame/1000,0.4*ones(length(frame60(:,index)),1), 'r^')
        plot((frame50(:,index)-1)*mSecForFrame/1000,0.5*ones(length(frame50(:,index)),1), 'rx')
        plot((frame30(:,index)-1)*mSecForFrame/1000,0.7*ones(length(frame30(:,index)),1), 'r+')
    end
    fileName1 = [outputfilename(1:(length(outputfilename)-8)) 'interested_traces' num2str(i) '.fig'];
    saveas(gcf, fileName1, 'fig');
    if(get(handles.autoClose, 'Value'))
        close gcf
    end
end
%mapping of duration

xsize = inputOrigin.xsize;
ysize = inputOrigin.ysize;
legendtextDuration = 'msec';
maxDuration = clim(2);
minDuration = clim(1);
mappingImageExportOmsLowMagV2_2(duration30_tAvg, xsize, ysize, [outputfilename '30'], legendtextDuration, clim, maxDuration, minDuration)
mappingImageExportOmsLowMagV2_2(duration50_tAvg, xsize, ysize, [outputfilename '50'], legendtextDuration, clim, maxDuration, minDuration)
mappingImageExportOmsLowMagV2_2(duration60_tAvg, xsize, ysize, [outputfilename '60'], legendtextDuration, clim, maxDuration, minDuration)
mappingImageExportOmsLowMagV2_2(duration80_tAvg, xsize, ysize, [outputfilename '80'], legendtextDuration, clim, maxDuration, minDuration)
mappingImageExportOmsLowMagV2_2(duration90_tAvg, xsize, ysize, [outputfilename '90'], legendtextDuration, clim, maxDuration, minDuration)

for k=1:nPulse
    mappingImageExportOmsLowMagV2_2coolhot(duration80(k,:), xsize, ysize, [outputfilename '80CH' num2str(k)], legendtextDuration, clim, maxDuration, minDuration)
end

% calculate global duration

canlocation = canlocation +1;

% for i=1:size(canlocation,1)
%     ParaAveraged = [1 100 canlocation(i,1) canlocation(i,2) 1 0 100 100];
%     temp30 = averagedOMSDataV2temp(duration30_tAvg,ParaAveraged);
%     temp50 = averagedOMSDataV2temp(duration50_tAvg,ParaAveraged);
%     temp80 = averagedOMSDataV2temp(duration80_tAvg,ParaAveraged);
%     temp90 = averagedOMSDataV2temp(duration90_tAvg,ParaAveraged);
%     outAverage30(i,:) = temp30.averagedData;
%     outAverage50(i,:) = temp50.averagedData;
%     outAverage80(i,:) = temp80.averagedData;
%     outAverage90(i,:) = temp90.averagedData;
%     
%     isNAN30(i) = sum(isnan(outAverage30(i, 1:interestedlocation)));
%     isNAN50(i) = sum(isnan(outAverage50(i, 1:interestedlocation)));
%     isNAN80(i) = sum(isnan(outAverage80(i, 1:interestedlocation)));
%     isNAN90(i) = sum(isnan(outAverage90(i, 1:interestedlocation)));
%     
%     if(isNAN30(i)>0)
%         YesNan30 = isnan(outAverage30(i, 1:interestedlocation));
%         outAverage30(i,find(YesNan30>0)) = 0;
%     end
%     if(isNAN50(i)>0)
%         YesNan50 = isnan(outAverage50(i, 1:interestedlocation));
%         outAverage50(i,find(YesNan50>0)) = 0;
%     end
%     if(isNAN80(i)>0)
%         YesNan80 = isnan(outAverage80(i, 1:interestedlocation));
%         outAverage80(i,find(YesNan80>0)) = 0;
%     end
%     if(isNAN90(i)>0)
%         YesNan90 = isnan(outAverage90(i, 1:interestedlocation));
%         outAverage90(i,find(YesNan90>0)) = 0;
%     end
%  
%     duration30_global(i) = mean(outAverage30(i, 1:interestedlocation))*interestedlocation/(interestedlocation-isNAN30(i));
%     duration50_global(i) = mean(outAverage50(i, 1:interestedlocation))*interestedlocation/(interestedlocation-isNAN50(i));
%     duration80_global(i) = mean(outAverage80(i, 1:interestedlocation))*interestedlocation/(interestedlocation-isNAN80(i));
%     duration90_global(i) = mean(outAverage90(i, 1:interestedlocation))*interestedlocation/(interestedlocation-isNAN90(i));
% 
% end



% calculate dispersion of duration 
outCantileverNtemp = zeros(xsize,ysize);
for i=1:size(canlocation,1)
    for jj = canlocation(i,1):canlocation(i,2)
        for ii=startPoint:interestedlocation
            outCantileverNtemp(ii,jj) = i; 
        end
    end
end
outCantileverN = reshape(outCantileverNtemp,1, xsize*ysize); 


for i=1:size(canlocation,1)
    tempCanN = find(outCantileverN == i);
    
    dataHisto30 = duration30_tAvg(1,tempCanN);
    dataHisto50 = duration50_tAvg(1,tempCanN);
    dataHisto80 = duration80_tAvg(1,tempCanN);
    dataHisto90 = duration90_tAvg(1,tempCanN);
    for j=1:length(dataHisto30)
        dataHisto30All(i,j) = dataHisto30(j);
    end
    for j=1:length(dataHisto50)
        dataHisto50All(i,j) = dataHisto50(j);
    end
    for j=1:length(dataHisto80)
        dataHisto80All(i,j) = dataHisto80(j);
    end
    for j=1:length(dataHisto90)
        dataHisto90All(i,j) = dataHisto90(j);
    end
    clear dataHisto30
    clear dataHisto50
    clear dataHisto80
    clear dataHisto90
    
    
    dataHisto70 = duration70_tAvg(1,tempCanN);
    dataHisto60 = duration60_tAvg(1,tempCanN);
    dataHisto40 = duration40_tAvg(1,tempCanN);
    dataHisto20 = duration20_tAvg(1,tempCanN);
    dataHisto10 = duration10_tAvg(1,tempCanN);
    for j=1:length(dataHisto70)
        dataHisto70All(i,j) = dataHisto70(j);
    end
    for j=1:length(dataHisto60)
        dataHisto60All(i,j) = dataHisto60(j);
    end
    for j=1:length(dataHisto40)
        dataHisto40All(i,j) = dataHisto40(j);
    end
    for j=1:length(dataHisto20)
        dataHisto20All(i,j) = dataHisto20(j);
    end
    for j=1:length(dataHisto10)
        dataHisto10All(i,j) = dataHisto10(j);
    end
    clear dataHisto70
    clear dataHisto60
    clear dataHisto40
    clear dataHisto20
    clear dataHisto10
    
    
    
    
    dataHistoAmplitude = rawAmplitude(1,tempCanN);
    for j=1:length(dataHistoAmplitude)
        dataHistoAmplitudeAll(i,j) = dataHistoAmplitude(j);
    end
    clear dataHistoAmplitude
end
dataMeaningless30 = find(dataHisto30All<=0);
dataMeaningless50 = find(dataHisto50All<=0);
dataMeaningless80 = find(dataHisto80All<=0);
dataMeaningless90 = find(dataHisto90All<=0);
dataHisto30All(dataMeaningless30) = NaN;
dataHisto50All(dataMeaningless50) = NaN;
dataHisto80All(dataMeaningless80) = NaN;
dataHisto90All(dataMeaningless90) = NaN;

dataMeaningless70 = find(dataHisto70All<=0);
dataMeaningless60 = find(dataHisto60All<=0);
dataMeaningless40 = find(dataHisto40All<=0);
dataMeaningless20 = find(dataHisto20All<=0);
dataMeaningless10 = find(dataHisto10All<=0);
dataHisto70All(dataMeaningless70) = NaN;
dataHisto60All(dataMeaningless60) = NaN;
dataHisto40All(dataMeaningless40) = NaN;
dataHisto20All(dataMeaningless20) = NaN;
dataHisto10All(dataMeaningless10) = NaN;

dataMeaninglessoAmplitude = find(dataHistoAmplitudeAll<=0);
dataHistoAmplitudeAll(dataMeaninglessoAmplitude) = NaN;

for i=1:size(canlocation,1)
    isNAN30(i,:) = isnan(dataHisto30All(i,:));
    duration30DataN(i) = size(isNAN30,2)-sum(isNAN30(i,:));
    [x30, y30, p30, q30] =  normfit(dataHisto30All(i,find(isNAN30(i,:)==0))');
    duration30Mean(i) = x30;
    duration30Std(i) = y30;
    duration30StdErr(i) = y30/(duration30DataN(i)^0.5);
    duration30MeanLimit(i,:) = p30;
    duration30StdLimit(i,:) = q30;
    duration30StdErrLimit(i,:) = q30/(duration30DataN(i)^0.5);
    
    
    isNAN50(i,:) = isnan(dataHisto50All(i,:));
    duration50DataN(i) = size(isNAN50,2)-sum(isNAN50(i,:));
    [x50, y50, p50, q50] =  normfit(dataHisto50All(i,find(isNAN50(i,:)==0))');
    duration50Mean(i) = x50;
    duration50Std(i) = y50;
    duration50StdErr(i) = y50/(duration50DataN(i)^0.5);
    duration50MeanLimit(i,:) = p50;
    duration50StdLimit(i,:) = q50;
    duration50StdErrLimit(i,:) = q50/(duration50DataN(i)^0.5);
    
    
    isNAN80(i,:) = isnan(dataHisto80All(i,:));
    duration80DataN(i) = size(isNAN80,2)-sum(isNAN80(i,:));
    [x80, y80, p80, q80] =  normfit(dataHisto80All(i,find(isNAN80(i,:)==0))');
    duration80Mean(i) = x80;
    duration80Std(i) = y80;
    duration80StdErr(i) = y80/(duration80DataN(i)^0.5);
    duration80MeanLimit(i,:) = p80;
    duration80StdLimit(i,:) = q80;
    duration80StdErrLimit(i,:) = q80/(duration80DataN(i)^0.5);
    
    
    isNAN90(i,:) = isnan(dataHisto90All(i,:));
    duration90DataN(i) = size(isNAN90,2)-sum(isNAN90(i,:));
    [x90, y90, p90, q90] =  normfit(dataHisto90All(i,find(isNAN90(i,:)==0))');
    duration90Mean(i) = x90;
    duration90Std(i) = y90;
    duration90StdErr(i) = y90/(duration90DataN(i)^0.5);
    duration90MeanLimit(i,:) = p90;
    duration90StdLimit(i,:) = q90;
    duration90StdErrLimit(i,:) = q90/(duration90DataN(i)^0.5);
    
    
    
    
    
    isNAN70(i,:) = isnan(dataHisto70All(i,:));
    duration70DataN(i) = size(isNAN70,2)-sum(isNAN70(i,:));
    [x70, y70, p70, q70] =  normfit(dataHisto70All(i,find(isNAN70(i,:)==0))');
    duration70Mean(i) = x70;
    duration70Std(i) = y70;
    duration70StdErr(i) = y70/(duration70DataN(i)^0.5);
    duration70MeanLimit(i,:) = p70;
    duration70StdLimit(i,:) = q70;
    duration70StdErrLimit(i,:) = q70/(duration70DataN(i)^0.5);
    
    
    isNAN60(i,:) = isnan(dataHisto60All(i,:));
    duration60DataN(i) = size(isNAN60,2)-sum(isNAN60(i,:));
    [x60, y60, p60, q60] =  normfit(dataHisto60All(i,find(isNAN60(i,:)==0))');
    duration60Mean(i) = x60;
    duration60Std(i) = y60;
    duration60StdErr(i) = y60/(duration60DataN(i)^0.5);
    duration60MeanLimit(i,:) = p60;
    duration60StdLimit(i,:) = q60;
    duration60StdErrLimit(i,:) = q60/(duration60DataN(i)^0.5);
    
    
    isNAN40(i,:) = isnan(dataHisto40All(i,:));
    duration40DataN(i) = size(isNAN40,2)-sum(isNAN40(i,:));
    [x40, y40, p40, q40] =  normfit(dataHisto40All(i,find(isNAN40(i,:)==0))');
    duration40Mean(i) = x40;
    duration40Std(i) = y40;
    duration40StdErr(i) = y40/(duration40DataN(i)^0.5);
    duration40MeanLimit(i,:) = p40;
    duration40StdLimit(i,:) = q40;
    duration40StdErrLimit(i,:) = q40/(duration40DataN(i)^0.5);
    
    
    isNAN20(i,:) = isnan(dataHisto20All(i,:));
    duration20DataN(i) = size(isNAN20,2)-sum(isNAN20(i,:));
    [x20, y20, p20, q20] =  normfit(dataHisto20All(i,find(isNAN20(i,:)==0))');
    duration20Mean(i) = x20;
    duration20Std(i) = y20;
    duration20StdErr(i) = y20/(duration20DataN(i)^0.5);
    duration20MeanLimit(i,:) = p20;
    duration20StdLimit(i,:) = q20;
    duration20StdErrLimit(i,:) = q20/(duration20DataN(i)^0.5);
    
    
    isNAN10(i,:) = isnan(dataHisto10All(i,:));
    duration10DataN(i) = size(isNAN10,2)-sum(isNAN10(i,:));
    [x10, y10, p10, q10] =  normfit(dataHisto10All(i,find(isNAN10(i,:)==0))');
    duration10Mean(i) = x10;
    duration10Std(i) = y10;
    duration10StdErr(i) = y10/(duration10DataN(i)^0.5);
    duration10MeanLimit(i,:) = p10;
    duration10StdLimit(i,:) = q10;
    duration10StdErrLimit(i,:) = q10/(duration10DataN(i)^0.5);
    
    
    
    
    
    
    
    isNANAmplitude(i,:) = isnan(dataHistoAmplitudeAll(i,:));
    amplitudeDataN(i) = size(isNANAmplitude,2)-sum(isNANAmplitude(i,:));
    [xA, yA, pA, qA] =  normfit(dataHistoAmplitudeAll(i,find(isNANAmplitude(i,:)==0))');
    amplitudeMean(i) = xA;
    amplitudeStd(i) = yA;
    amplitudeStdErr(i) = yA/(amplitudeDataN(i)^0.5);
    amplitudeMeanLimit(i,:) = pA;
    amplitudeStdLimit(i,:) = qA;
    amplitudeStdErrLimit(i,:) = qA/(amplitudeDataN(i)^0.5);
end

figure(203);
for i=1:size(canlocation,1)
    subplot(2,2,i),
    hist(dataHisto30All(i,:)', 0:5:clim(2))
    box off
    xlabel('Duration30 (msec)', 'fontsize', 14)
    ylabel('Count', 'fontsize', 14)
    set(gca,'fontsize',12)
    xlim([minDuration maxDuration])
end

fileName1 = [outputfilename(1:(length(outputfilename)-8)) 'Histo30.fig'];
saveas(gcf, fileName1, 'fig');
if(get(handles.autoClose, 'Value'))
    close figure 203
end

figure(204);
for i=1:size(canlocation,1)
    subplot(2,2,i),
    hist(dataHisto50All(i,:)', 0:5:clim(2))
    box off
    xlabel('Duration50 (msec)', 'fontsize', 14)
    ylabel('Count', 'fontsize', 14)
    set(gca,'fontsize',12)
    xlim([minDuration maxDuration])
end

fileName1 = [outputfilename(1:(length(outputfilename)-8)) 'Histo50.fig'];
saveas(gcf, fileName1, 'fig');
if(get(handles.autoClose, 'Value'))
    close figure 204
end

figure(214);
for i=1:size(canlocation,1)
    subplot(2,2,i),
    hist(dataHisto60All(i,:)', 0:5:clim(2))
    box off
    xlabel('Duration50 (msec)', 'fontsize', 14)
    ylabel('Count', 'fontsize', 14)
    set(gca,'fontsize',12)
    xlim([minDuration maxDuration])
end

fileName1 = [outputfilename(1:(length(outputfilename)-8)) 'Histo60.fig'];
saveas(gcf, fileName1, 'fig');
if(get(handles.autoClose, 'Value'))
    close figure 214
end

figure(205);
for i=1:size(canlocation,1)
    subplot(2,2,i),
    hist(dataHisto80All(i,:)', 0:5:clim(2))
    box off
    xlabel('Duration80 (msec)', 'fontsize', 14)
    ylabel('Count', 'fontsize', 14)
    set(gca,'fontsize',12)
    xlim([minDuration maxDuration])
end

fileName1 = [outputfilename(1:(length(outputfilename)-8)) 'Histo80.fig'];
saveas(gcf, fileName1, 'fig');
if(get(handles.autoClose, 'Value'))
    close figure 205
end

figure(206);
for i=1:size(canlocation,1)
    subplot(2,2,i),
    hist(dataHisto90All(i,:)', 0:5:clim(2))
    box off
    xlabel('Duration90 (msec)', 'fontsize', 14)
    ylabel('Count', 'fontsize', 14)
    set(gca,'fontsize',12)
    xlim([minDuration maxDuration])
end

fileName1 = [outputfilename(1:(length(outputfilename)-8)) 'Histo90.fig'];
saveas(gcf, fileName1, 'fig');
if(get(handles.autoClose, 'Value'))
    close figure 206
end


figure(207);
nPlotBin = 0:5:clim(2);
for i=1:size(canlocation,1)
    subplot(2,2,i),
    %hist(dataHisto30All(i,:)', nPlotBin)
    [n30 xout30] = hist(dataHisto30All(i,:)', nPlotBin);
    bar(xout30, n30, 'm')
    hold on
    box off
    xlabel('Duration (msec)', 'fontsize', 14)
    ylabel('Count', 'fontsize', 14)
    set(gca,'fontsize',12)
    %h = findobj(gca,'Type','patch');
    %set(h(1),'Facecolor',[1 0 0],'EdgeColor','k');
    xlim([minDuration maxDuration])
end
for i=1:size(canlocation,1)
    subplot(2,2,i),
    %hist(dataHisto50All(i,:)', nPlotBin)
    %h = findobj(gca,'Type','patch');
    %set(h(2),'Facecolor',[0 0 1],'EdgeColor','k');
    [n50 xout50] = hist(dataHisto50All(i,:)', nPlotBin);
    bar(xout50, n50, 'r')
end
for i=1:size(canlocation,1)
    subplot(2,2,i),
    %hist(dataHisto80All(i,:)', nPlotBin)
    [n80 xout80] = hist(dataHisto80All(i,:)', nPlotBin);
    bar(xout80, n80, 'g')
end
for i=1:size(canlocation,1)
    subplot(2,2,i),
    %hist(dataHisto90All(i,:)', nPlotBin)
    [n90 xout90] = hist(dataHisto90All(i,:)', nPlotBin);
    bar(xout90, n90, 'b')
end
fileName1 = [outputfilename(1:(length(outputfilename)-8)) 'HistoAll.fig'];
saveas(gcf, fileName1, 'fig');
if(get(handles.autoClose, 'Value'))
    close figure 207
end


figure(208);
for i=1:size(canlocation,1)
    subplot(2,2,i),
    hist(dataHistoAmplitudeAll(i,:)', 0:0.005:0.5)
    box off
    xlabel('Amplitude', 'fontsize', 14)
    ylabel('Count', 'fontsize', 14)
    set(gca,'fontsize',12)
    xlim([0 0.5])
end

fileName1 = [outputfilename(1:(length(outputfilename)-8)) 'HistoAmp.fig'];
saveas(gcf, fileName1, 'fig');
if(get(handles.autoClose, 'Value'))
    close figure 208
end





%mapping of duration Z-score

xsize = inputOrigin.xsize;
ysize = inputOrigin.ysize;
legendtextZ = 'z-score';
maxZ = 5;
minZ = -5;
climZ = [minZ maxZ];
    
for i=1:3 %size(canlocation,1)
%     if(~isnan(duration30Mean(i)))
%         mappingImageExportOmsLowMagV2_2coolhot((duration30_tAvg-duration30Mean(i))/duration30Std(i), xsize, ysize, [outputfilename '30_can' num2str(i)], legendtextZ, climZ, maxZ, minZ)
%     end
%     if(~isnan(duration50Mean(i)))
%         mappingImageExportOmsLowMagV2_2coolhot((duration50_tAvg-duration50Mean(i))/duration50Std(i), xsize, ysize, [outputfilename '50_can' num2str(i)], legendtextZ, climZ, maxZ, minZ)
%     end
    if(~isnan(duration60Mean(i)))
        mappingImageExportOmsLowMagV2_2coolhot((duration60_tAvg-duration60Mean(i))/duration60Std(i), xsize, ysize, [outputfilename '60_can' num2str(i)], legendtextZ, climZ, maxZ, minZ)
    end
    if(~isnan(duration80Mean(i)))
        mappingImageExportOmsLowMagV2_2coolhot((duration80_tAvg-duration80Mean(i))/duration80Std(i), xsize, ysize, [outputfilename '80_can' num2str(i)], legendtextZ, climZ, maxZ, minZ)
    end
%     if(~isnan(duration90Mean(i)))
% %        mappingImageExportOmsLowMagV2_2coolhot((duration90_tAvg-duration90Mean(i))/duration90Std(i), xsize, ysize, [outputfilename '90_can' num2str(i)], legendtextZ, climZ, maxZ, minZ)
%     end
end 





% calculate instability of duration

for i=1:size(canlocation,1)
    tempCanN = find(outCantileverN == i);
    
    dataHisto30I = duration30(:,tempCanN);
    dataHisto50I = duration50(:,tempCanN);
    dataHisto80I = duration80(:,tempCanN);
    dataHisto90I = duration90(:,tempCanN);
    
    dataMeaningless30I = find(dataHisto30I<=0);
    dataMeaningless50I = find(dataHisto50I<=0);
    dataMeaningless80I = find(dataHisto80I<=0);
    dataMeaningless90I = find(dataHisto90I<=0);
    dataHisto30I(dataMeaningless30I) = NaN;
    dataHisto50I(dataMeaningless50I) = NaN;
    dataHisto80I(dataMeaningless80I) = NaN;
    dataHisto90I(dataMeaningless90I) = NaN;
    
    
    dataHisto70I = duration70(:,tempCanN);
    dataHisto60I = duration60(:,tempCanN);
    dataHisto40I = duration40(:,tempCanN);
    dataHisto20I = duration20(:,tempCanN);
    dataHisto10I = duration10(:,tempCanN);
    
    dataMeaningless70I = find(dataHisto70I<=0);
    dataMeaningless60I = find(dataHisto60I<=0);
    dataMeaningless40I = find(dataHisto40I<=0);
    dataMeaningless20I = find(dataHisto20I<=0);
    dataMeaningless10I = find(dataHisto10I<=0);
    dataHisto70I(dataMeaningless70I) = NaN;
    dataHisto60I(dataMeaningless60I) = NaN;
    dataHisto40I(dataMeaningless40I) = NaN;
    dataHisto20I(dataMeaningless20I) = NaN;
    dataHisto10I(dataMeaningless10I) = NaN;
    
    for kk = 1:nPulse
        
        isNAN30I(kk,:) = isnan(dataHisto30I(kk,:));
        isNAN50I(kk,:) = isnan(dataHisto50I(kk,:));
        isNAN80I(kk,:) = isnan(dataHisto80I(kk,:));
        isNAN90I(kk,:) = isnan(dataHisto90I(kk,:));
        
        dataInstablity30(i,kk) =  mean(dataHisto30I(kk,find(isNAN30I(kk,:)==0))');
        dataInstablity50(i,kk) =  mean(dataHisto50I(kk,find(isNAN50I(kk,:)==0))');
        dataInstablity80(i,kk) =  mean(dataHisto80I(kk,find(isNAN80I(kk,:)==0))');
        dataInstablity90(i,kk) =  mean(dataHisto90I(kk,find(isNAN90I(kk,:)==0))');
        
        
        isNAN70I(kk,:) = isnan(dataHisto70I(kk,:));
        isNAN60I(kk,:) = isnan(dataHisto60I(kk,:));
        isNAN40I(kk,:) = isnan(dataHisto40I(kk,:));
        isNAN20I(kk,:) = isnan(dataHisto20I(kk,:));
        isNAN10I(kk,:) = isnan(dataHisto10I(kk,:));
        
        dataInstablity70(i,kk) =  mean(dataHisto70I(kk,find(isNAN70I(kk,:)==0))');
        dataInstablity60(i,kk) =  mean(dataHisto60I(kk,find(isNAN60I(kk,:)==0))');
        dataInstablity40(i,kk) =  mean(dataHisto40I(kk,find(isNAN40I(kk,:)==0))');
        dataInstablity20(i,kk) =  mean(dataHisto20I(kk,find(isNAN20I(kk,:)==0))');
        dataInstablity10(i,kk) =  mean(dataHisto10I(kk,find(isNAN10I(kk,:)==0))');
    end
    
    
    clear dataHisto30I
    clear dataHisto50I
    clear dataHisto80I
    clear dataHisto90I
    
    clear isNAN30I
    clear isNAN50I
    clear isNAN80I
    clear isNAN90I
    
    
    clear dataHisto70I
    clear dataHisto60I
    clear dataHisto40I
    clear dataHisto20I
    clear dataHisto10I
    
    clear isNAN70I
    clear isNAN60I
    clear isNAN40I
    clear isNAN20I
    clear isNAN10I
    
end

data50_report = dataInstablity50
data80_report = dataInstablity80

for i=1:size(canlocation,1)
    [x30, y30, p30, q30] =  normfit(dataInstablity30(i,:)');
    duration30IMean(i) = x30;
    duration30IStd(i) = y30;
    duration30IStdErr(i) = y30/(nPulse^0.5);
    duration30IMeanLimit(i,:) = p30;
    duration30IStdLimit(i,:) = q30;
    duration30IStdErrLimit(i,:) = q30/(nPulse^0.5);
    
    [x50, y50, p50, q50] =  normfit(dataInstablity50(i,:)');
    duration50IMean(i) = x50;
    duration50IStd(i) = y50;
    duration50IStdErr(i) = y50/(nPulse^0.5);
    duration50IMeanLimit(i,:) = p50;
    duration50IStdLimit(i,:) = q50;
    duration50IStdErrLimit(i,:) = q50/(nPulse^0.5);
    
    [x80, y80, p80, q80] =  normfit(dataInstablity80(i,:)');
    duration80IMean(i) = x80;
    duration80IStd(i) = y80;
    duration80IStdErr(i) = y80/(nPulse^0.5);
    duration80IMeanLimit(i,:) = p80;
    duration80IStdLimit(i,:) = q80;
    duration80IStdErrLimit(i,:) = q80/(nPulse^0.5);
    
    [x90, y90, p90, q90] =  normfit(dataInstablity90(i,:)');
    duration90IMean(i) = x90;
    duration90IStd(i) = y90;
    duration90IStdErr(i) = y90/(nPulse^0.5);
    duration90IMeanLimit(i,:) = p90;
    duration90IStdLimit(i,:) = q90;
    duration90IStdErrLimit(i,:) = q90/(nPulse^0.5);
    
    
    
    
    
    
    [x70, y70, p70, q70] =  normfit(dataInstablity70(i,:)');
    duration70IMean(i) = x70;
    duration70IStd(i) = y70;
    duration70IStdErr(i) = y70/(nPulse^0.5);
    duration70IMeanLimit(i,:) = p70;
    duration70IStdLimit(i,:) = q70;
    duration70IStdErrLimit(i,:) = q70/(nPulse^0.5);
    
    [x60, y60, p60, q60] =  normfit(dataInstablity60(i,:)');
    duration60IMean(i) = x60;
    duration60IStd(i) = y60;
    duration60IStdErr(i) = y60/(nPulse^0.5);
    duration60IMeanLimit(i,:) = p60;
    duration60IStdLimit(i,:) = q60;
    duration60IStdErrLimit(i,:) = q60/(nPulse^0.5);
    
    [x40, y40, p40, q40] =  normfit(dataInstablity40(i,:)');
    duration40IMean(i) = x40;
    duration40IStd(i) = y40;
    duration40IStdErr(i) = y40/(nPulse^0.5);
    duration40IMeanLimit(i,:) = p40;
    duration40IStdLimit(i,:) = q40;
    duration40IStdErrLimit(i,:) = q40/(nPulse^0.5);
    
    [x20, y20, p20, q20] =  normfit(dataInstablity20(i,:)');
    duration20IMean(i) = x20;
    duration20IStd(i) = y20;
    duration20IStdErr(i) = y20/(nPulse^0.5);
    duration20IMeanLimit(i,:) = p20;
    duration20IStdLimit(i,:) = q20;
    duration20IStdErrLimit(i,:) = q20/(nPulse^0.5);
    
    [x10, y10, p10, q10] =  normfit(dataInstablity10(i,:)');
    duration10IMean(i) = x10;
    duration10IStd(i) = y10;
    duration10IStdErr(i) = y10/(nPulse^0.5);
    duration10IMeanLimit(i,:) = p10;
    duration10IStdLimit(i,:) = q10;
    duration10IStdErrLimit(i,:) = q10/(nPulse^0.5);

end

figure(209);
for i=1:size(canlocation,1)
    subplot(2,2,i),
    plot(dataInstablity30(i,1:(nPulse-1)), dataInstablity30(i,2:nPulse), 'b')
    hold on
    plot(dataInstablity50(i,1:(nPulse-1)), dataInstablity50(i,2:nPulse), 'r')
    plot(dataInstablity60(i,1:(nPulse-1)), dataInstablity60(i,2:nPulse), 'c')
    plot(dataInstablity80(i,1:(nPulse-1)), dataInstablity80(i,2:nPulse), 'g')
    plot(dataInstablity90(i,1:(nPulse-1)), dataInstablity90(i,2:nPulse), 'm')
    box off
    xlabel('Duration (msec)', 'fontsize', 14)
    ylabel('Duration (msec)', 'fontsize', 14)
    set(gca,'fontsize',12)
    xlim([minDuration maxDuration])
    ylim([minDuration maxDuration])
end

fileName1 = [outputfilename(1:(length(outputfilename)-8)) 'PoinCareDuration.fig'];
saveas(gcf, fileName1, 'fig');
if(get(handles.autoClose, 'Value'))
    close figure 209
end

duration30IInstability = calculateInstabilityPoinCareV2(dataInstablity30);
duration50IInstability = calculateInstabilityPoinCareV2(dataInstablity50);
duration80IInstability = calculateInstabilityPoinCareV2(dataInstablity80);
duration90IInstability = calculateInstabilityPoinCareV2(dataInstablity90);

duration10IInstability = calculateInstabilityPoinCareV2(dataInstablity10);
duration20IInstability = calculateInstabilityPoinCareV2(dataInstablity20);
duration40IInstability = calculateInstabilityPoinCareV2(dataInstablity40);
duration60IInstability = calculateInstabilityPoinCareV2(dataInstablity60);
duration70IInstability = calculateInstabilityPoinCareV2(dataInstablity70);


out.cantileverN = outCantileverN;

out.rawData90Check = rawData90Check;
out.rawData80Check = rawData80Check;
out.rawData60Check = rawData60Check;
out.rawData50Check = rawData50Check;
out.rawData30Check = rawData30Check;
out.frame90 = frame90;
out.frame80 = frame80;
out.frame60 = frame60;
out.frame50 = frame50;
out.frame30 = frame30;
out.frame90_1 = frame90_1;
out.frame80_1 = frame80_1;
out.frame60_1 = frame60_1;
out.frame50_1 = frame50_1;
out.frame30_1 = frame30_1;

out.duration90 = duration90; %(beat, 10000 pixel)
out.duration80 = duration80;
out.duration60 = duration60;
out.duration50 = duration50;
out.duration30 = duration30;
out.duration90_tAvg = duration90_tAvg;
out.duration80_tAvg = duration80_tAvg;
out.duration60_tAvg = duration60_tAvg;
out.duration50_tAvg = duration50_tAvg;
out.duration30_tAvg = duration30_tAvg;

% out.duration90_global = duration90_global;
% out.duration80_global = duration80_global;
% out.duration50_global = duration50_global;
% out.duration30_global = duration30_global;




out.duration30DataN = duration30DataN;
out.duration30Mean = duration30Mean;
out.duration30Std = duration30Std;
out.duration30StdErr = duration30StdErr;
out.duration30MeanLimit = duration30MeanLimit;
out.duration30StdLimit = duration30StdLimit;
out.duration30StdErrLimit = duration30StdErrLimit;
out.duration30CV = duration30Std./duration30Mean; %Coefficient of variation

out.duration50DataN = duration50DataN;
out.duration50Mean = duration50Mean;
out.duration50Std = duration50Std;
out.duration50StdErr = duration50StdErr;
out.duration50MeanLimit = duration50MeanLimit;
out.duration50StdLimit = duration50StdLimit;
out.duration50StdErrLimit = duration50StdErrLimit;
out.duration50CV = duration50Std./duration50Mean; %Coefficient of variation

out.duration80DataN = duration80DataN;
out.duration80Mean = duration80Mean;
out.duration80Std = duration80Std;
out.duration80StdErr = duration80StdErr;
out.duration80MeanLimit = duration80MeanLimit;
out.duration80StdLimit = duration80StdLimit;
out.duration80StdErrLimit = duration80StdErrLimit;
out.duration80CV = duration80Std./duration80Mean; %Coefficient of variation

out.duration90DataN = duration90DataN;
out.duration90Mean = duration90Mean;
out.duration90Std = duration90Std;
out.duration90StdErr = duration90StdErr;
out.duration90MeanLimit = duration90MeanLimit;
out.duration90StdLimit = duration90StdLimit;
out.duration90StdErrLimit = duration90StdErrLimit;
out.duration90CV = duration90Std./duration90Mean; %Coefficient of variation




out.duration70DataN = duration70DataN;
out.duration70Mean = duration70Mean;
out.duration70Std = duration70Std;
out.duration70StdErr = duration70StdErr;
out.duration70MeanLimit = duration70MeanLimit;
out.duration70StdLimit = duration70StdLimit;
out.duration70StdErrLimit = duration70StdErrLimit;
out.duration70CV = duration70Std./duration70Mean; %Coefficient of variation

out.duration60DataN = duration60DataN;
out.duration60Mean = duration60Mean;
out.duration60Std = duration60Std;
out.duration60StdErr = duration60StdErr;
out.duration60MeanLimit = duration60MeanLimit;
out.duration60StdLimit = duration60StdLimit;
out.duration60StdErrLimit = duration60StdErrLimit;
out.duration60CV = duration60Std./duration60Mean; %Coefficient of variation

out.duration40DataN = duration40DataN;
out.duration40Mean = duration40Mean;
out.duration40Std = duration40Std;
out.duration40StdErr = duration40StdErr;
out.duration40MeanLimit = duration40MeanLimit;
out.duration40StdLimit = duration40StdLimit;
out.duration40StdErrLimit = duration40StdErrLimit;
out.duration40CV = duration40Std./duration40Mean; %Coefficient of variation

out.duration20DataN = duration20DataN;
out.duration20Mean = duration20Mean;
out.duration20Std = duration20Std;
out.duration20StdErr = duration20StdErr;
out.duration20MeanLimit = duration20MeanLimit;
out.duration20StdLimit = duration20StdLimit;
out.duration20StdErrLimit = duration20StdErrLimit;
out.duration20CV = duration20Std./duration20Mean; %Coefficient of variation

out.duration10DataN = duration10DataN;
out.duration10Mean = duration10Mean;
out.duration10Std = duration10Std;
out.duration10StdErr = duration10StdErr;
out.duration10MeanLimit = duration10MeanLimit;
out.duration10StdLimit = duration10StdLimit;
out.duration10StdErrLimit = duration10StdErrLimit;
out.duration10CV = duration10Std./duration10Mean; %Coefficient of variation





out.duration30IDataN = nPulse*ones(1,size(canlocation,1));
out.duration30IMean = duration30IMean;
out.duration30IStd = duration30IStd;
out.duration30IStdErr = duration30IStdErr;
out.duration30IMeanLimit = duration30IMeanLimit;
out.duration30IStdLimit = duration30IStdLimit;
out.duration30IStdErrLimit = duration30IStdErrLimit;
out.duration30ICV = duration30IStd./duration30IMean; %Coefficient of variation
out.duration30IInstability = duration30IInstability;

out.duration50IDataN = nPulse*ones(1,size(canlocation,1));
out.duration50IMean = duration50IMean;
out.duration50IStd = duration50IStd;
out.duration50IStdErr = duration50IStdErr;
out.duration50IMeanLimit = duration50IMeanLimit;
out.duration50IStdLimit = duration50IStdLimit;
out.duration50IStdErrLimit = duration50IStdErrLimit;
out.duration50ICV = duration50IStd./duration50IMean; %Coefficient of variation
out.duration50IInstability = duration50IInstability;

out.duration80IDataN = nPulse*ones(1,size(canlocation,1));
out.duration80IMean = duration80IMean;
out.duration80IStd = duration80IStd;
out.duration80IStdErr = duration80IStdErr;
out.duration80IMeanLimit = duration80IMeanLimit;
out.duration80IStdLimit = duration80IStdLimit;
out.duration80IStdErrLimit = duration80IStdErrLimit;
out.duration80ICV = duration80IStd./duration80IMean; %Coefficient of variation
out.duration80IInstability = duration80IInstability;

out.duration90IDataN = nPulse*ones(1,size(canlocation,1));
out.duration90IMean = duration90IMean;
out.duration90IStd = duration90IStd;
out.duration90IStdErr = duration90IStdErr;
out.duration90IMeanLimit = duration90IMeanLimit;
out.duration90IStdLimit = duration90IStdLimit;
out.duration90IStdErrLimit = duration90IStdErrLimit;
out.duration90ICV = duration90IStd./duration90IMean; %Coefficient of variation
out.duration90IInstability = duration90IInstability;





out.duration70IDataN = nPulse*ones(1,size(canlocation,1));
out.duration70IMean = duration70IMean;
out.duration70IStd = duration70IStd;
out.duration70IStdErr = duration70IStdErr;
out.duration70IMeanLimit = duration70IMeanLimit;
out.duration70IStdLimit = duration70IStdLimit;
out.duration70IStdErrLimit = duration70IStdErrLimit;
out.duration70ICV = duration70IStd./duration70IMean; %Coefficient of variation
out.duration70IInstability = duration70IInstability;

out.duration60IDataN = nPulse*ones(1,size(canlocation,1));
out.duration60IMean = duration60IMean;
out.duration60IStd = duration60IStd;
out.duration60IStdErr = duration60IStdErr;
out.duration60IMeanLimit = duration60IMeanLimit;
out.duration60IStdLimit = duration60IStdLimit;
out.duration60IStdErrLimit = duration60IStdErrLimit;
out.duration60ICV = duration60IStd./duration60IMean; %Coefficient of variation
out.duration60IInstability = duration60IInstability;

out.duration40IDataN = nPulse*ones(1,size(canlocation,1));
out.duration40IMean = duration40IMean;
out.duration40IStd = duration40IStd;
out.duration40IStdErr = duration40IStdErr;
out.duration40IMeanLimit = duration40IMeanLimit;
out.duration40IStdLimit = duration40IStdLimit;
out.duration40IStdErrLimit = duration40IStdErrLimit;
out.duration40ICV = duration40IStd./duration40IMean; %Coefficient of variation
out.duration40IInstability = duration40IInstability;

out.duration20IDataN = nPulse*ones(1,size(canlocation,1));
out.duration20IMean = duration20IMean;
out.duration20IStd = duration20IStd;
out.duration20IStdErr = duration20IStdErr;
out.duration20IMeanLimit = duration20IMeanLimit;
out.duration20IStdLimit = duration20IStdLimit;
out.duration20IStdErrLimit = duration20IStdErrLimit;
out.duration20ICV = duration20IStd./duration20IMean; %Coefficient of variation
out.duration20IInstability = duration20IInstability;

out.duration10IDataN = nPulse*ones(1,size(canlocation,1));
out.duration10IMean = duration10IMean;
out.duration10IStd = duration10IStd;
out.duration10IStdErr = duration10IStdErr;
out.duration10IMeanLimit = duration10IMeanLimit;
out.duration10IStdLimit = duration10IStdLimit;
out.duration10IStdErrLimit = duration10IStdErrLimit;
out.duration10ICV = duration10IStd./duration10IMean; %Coefficient of variation
out.duration10IInstability = duration10IInstability;








out.amplitudeDataN = amplitudeDataN;
out.amplitudeMean = amplitudeMean;
out.amplitudeStd = amplitudeStd;
out.amplitudeStdErr = amplitudeStdErr;
out.amplitudeMeanLimit = amplitudeMeanLimit;
out.amplitudeStdLimit = amplitudeStdLimit;
out.amplitudeStdErrLimit = amplitudeStdErrLimit;
out.amplitudeCV = amplitudeStd./amplitudeMean; %Coefficient of variation
